<?php

namespace app\index\controller;
use app\index\model\Product as ProductModel;
use think\Request;
use think\Log;
use think\Db;

class Product extends Base
{
    //产品列表
    public function  productList(Request $request)
    {
        $code = $request -> param('code');

        //获取所有产品表数据
        if(empty($code)){
            $product = Db::query('select a.*,ifnull(b.name,\'<span style="color:red;">未分配</span>\') as stepName from product a left join step b on a.id = b.product_id order by a.name');
            //$product = ProductModel::with('profile')->select();

        }else{
            //$product = ProductModel::all(['code'=>$code]);
            $sql = "select a.*,ifnull(b.name,'<span style=\"color:red;\">未分配</span>') as stepName from product a left join step b on a.id = b.product_id where a.code = '$code' order by a.name";
            $product = Db::query($sql);
        }
        Log::record('$sql:'.var_export($product,true),'info');
        $count = count($product);
        //获取记录数量
/*        if(empty($code)){
            $count = ProductModel::count();
        }else{
            $count = ProductModel::count(['code'=>$code]);
        }
        $count = ProductModel::count();*/

        $this -> view -> assign('productList', $product);
        $this -> view -> assign('count', $count);

        return $this -> view -> fetch('product_list');
    }

    //产品状态变更
    public function setStatus(Request $request)
    {
        //获取当前的产品ID
        $product_id = $request -> param('id');

        //查询
        $result = ProductModel::get($product_id);

        //启用和禁用处理
        if($result->getData('status') == 1) {
            ProductModel::update(['status'=>0],['id'=>$product_id]);
        } else {
            ProductModel::update(['status'=>1],['id'=>$product_id]);
        }
    }

    //渲染产品编辑界面
    public function productEdit(Request $request)
    {
        //获取到要编辑的产品ID
        $product_id = $request -> param('id');

        //根据ID进行查询
        $result = ProductModel::get($product_id);

        //给当前页面seo变量赋值
        $this->view->assign('title','编辑产品');
        $this->view->assign('keywords','rice.cn');
        $this->view->assign('desc','水稻产品管理系统');

        //给当前编辑模板赋值
        $this->view->assign('product_info',$result);

        //渲染编辑模板
        return $this->view->fetch('product_edit');
    }

    //产品更新
    public function doEdit(Request $request)
    {
        //从提交表单中排除关联字段product字段
        //$data = $request -> except('product');
        $data = $request -> param(); // 如果全部获取,提交会提示缺少字段product

        //设置更新条件
        $condition = ['id'=>$data['id']];

        //更新当前记录
        $result = ProductModel::update($data,$condition);

        //设置返回数据
        $status = 0;
        $message = '更新失败,请检查';

        //检测更新结果,将结果返回给product_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 更新成功~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    //渲染产品添加界面
    public function productAdd()
    {
        //给模板赋值seo变量
        $this->view->assign('title','编辑产品');
        $this->view->assign('keywords','rice.cn');
        $this->view->assign('desc','水稻产品管理系统');

        //渲染添加模板
        return $this->view->fetch('product_add');
    }

    //添加产品
    public function doAddStep(Request $request)
    {
        //从提交表单中获取数据
        $data = $request -> param();
        $data['code'] = uniqid();

        //更新当前记录
        $result = ProductModel::create($data);

        //设置返回数据的初始值
        $status = 0;
        $message = '添加失败,请检查';

        //检测更新结果,将结果返回给product_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 添加成功~~';
        }

        //自动转为json格式返回
        return ['status'=>$status, 'message'=>$message];
    }

    //添加产品追溯信息
    public function addStep(Request $request)
    {
        //获取到要编辑的产品ID
        $product_id = $request -> param('id');

        //根据ID进行查询
        $result = ProductModel::get($product_id);

        //给模板赋值seo变量
        $this->view->assign('title','添加产品追溯信息');
        $this->view->assign('keywords','rice.cn');
        $this->view->assign('desc','水稻产品管理系统');

        //给当前编辑模板赋值
        $this->view->assign('product_info',$result);

        //渲染添加模板
        return $this->view->fetch('product_step');
    }

    //软删除操作
    public function deleteProduct(Request $request)
    {
        $user_id = $request -> param('id');
        ProductModel::update(['is_delete'=>1],['id'=> $user_id]);
        ProductModel::destroy($user_id);

    }

    //恢复删除操作
    public function unDelete()
    {
        ProductModel::update(['delete_time'=>NULL],['is_delete'=>1]);
    }

}